package com.jz.netthings.bloodsources.service.impl;

import com.jz.netthings.application.bodyCheck.bean.MyPage;
import com.jz.netthings.bean.system.JyRole;
import com.jz.netthings.bloodsources.bean.XyLink;
import com.jz.netthings.bloodsources.bean.XyRemind;
import com.jz.netthings.bloodsources.constant.BloodSourcesConstant;
import com.jz.netthings.bloodsources.mapper.XyLinkMapper;
import com.jz.netthings.bloodsources.mapper.XyRemindMapper;
import com.jz.netthings.bloodsources.service.XyRemindService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 献浆提醒记录 服务实现类
 * </p>
 *
 * @author 张凡
 * @since 2019-08-03
 */
@Service
public class XyRemindServiceImpl extends ServiceImpl<XyRemindMapper, XyRemind> implements XyRemindService {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    private XyRemindMapper xyRemindMapper;
    @Override
    public Boolean insertXyRemindInfo(List<XyRemind> xyRemindList, Token token) throws AdscmSystemException {
        boolean b = super.insertBatch(xyRemindList);
        return b;
    }

    @Override
    public MyPage<XyRemind> queryByCondition(HttpServletRequest request,
                                             String overRemindTimeLess,
                                             String overRemindTimeGreater,
                                             String remindDateLess,
                                             String remindDateGreater,
                                             String remindNurseName,
                                             String remindProviderName,
                                             Integer remindStatus,
                                             String currentPage,
                                             String pageSize) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<JyRole> jyRoles = token.getUser().getRoles();
        String remindNurseDeptId = null;
        Integer remindNurseId = null;
        if(jyRoles!=null && jyRoles.size()>=1){
            //证明该登录用户有角色信息，则进行其拥有角色遍历查找，查看其是否拥有相关科室主任角色
            Boolean isDirector = false;
            Boolean isMaster = false;
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_MASTER_CODE.equals(jyRole.getRole_code())){     //如果是站长则直接条件查询
                    isMaster = true;
                    break;
                }
            }
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_DIRECTOR_CODE.equals(jyRole.getRole_code())){  //如果包含科室主任角色
                    isDirector = true;
                    break;
                }
            }
            // a.如果当前登录用户拥有   【科室主任角色】 ==》则获取其所属科室ID==》通过其科室ID（userDeptId = token.getUser().getDeptId();） 获取数据表同属其科室（同deptId）的所有血源链接护士的数据，并可以修改
            if(isMaster){
                //如果有站长就查询所有，则不给条件参数 remindNurseDeptId ，remindNurseId 赋约束值
            }else{
                //如果不是站长就按科室主任，护士权限判定流程走
                if(isDirector){
                    Integer userDeptId = token.getUser().getDeptId();  //用户部门Id
                    remindNurseDeptId = userDeptId+"";
                }else{
                    // b.如果当前登录用户未拥有 【科室主任角色】 ==》则获取其用户ID(token.getUser().getId())==>>通过其护士Id(remindNurseId《==》userId) 进行数据条件拉取
                    Integer userId = token.getUser().getId();
                    remindNurseId = userId;
                }
            }
        }else {

        }
        //进行分页数据判定
        MyPage<XyRemind> myPage=new MyPage<XyRemind>();
        myPage.setCurrentPageIndex(Integer.parseInt(currentPage));
        myPage.setPageSize(Integer.parseInt(pageSize));
        //对当前页参数currentPage 进行重新定义，为节省JVM资源开销，直接将计算后的开始页赋值给参数变量currentPage = beginIndex
        currentPage = myPage.getBeginIndex()+"";
        Integer count = xyRemindMapper.queryByCount(remindNurseId,remindNurseDeptId,overRemindTimeLess,overRemindTimeGreater,remindDateLess,remindDateGreater,remindNurseName,remindProviderName,remindStatus);
        List<XyRemind> list  = xyRemindMapper.queryByCondition(remindNurseId,remindNurseDeptId,overRemindTimeLess,overRemindTimeGreater,remindDateLess,remindDateGreater,remindNurseName,remindProviderName,remindStatus,remindStatus.parseInt(currentPage),Integer.parseInt(pageSize));
        //获取相关数据
        myPage.setItems(list);
        myPage.setTotal(count);
        return myPage;
    }
    @Override
    public Boolean updateXyRemindInfo(XyRemind xyRemind) throws AdscmSystemException {
        boolean b = super.updateById(xyRemind);
        return b;
    }

    @Override
    public List<XyRemind> loadXyRemindToCache() throws AdscmSystemException {
        return xyRemindMapper.loadXyRemindToCache();
    }
}
